package com.nulldev.util.web.HttpServer3.extensions.fakeFlare;

import com.nulldev.util.VariableAPI.MathUtil;
import com.nulldev.util.VariableAPI.RandomUtil;
import com.nulldev.util.web.HttpServer3.Response;
import com.nulldev.util.web.HttpServer3.pages.v3.HttpPage3;
import com.nulldev.util.web.URLUtils.URLUtils;

public class CDNCGITracePage extends HttpPage3 {

	public CDNCGITracePage() {
		super("/cdn-cgi/trace", true);
		super.setCachingMethod(CachingMethod.NO_CACHE);
	}

	@Override
	public Response onRequest(final Request request) {
		final StringBuilder out = new StringBuilder(320);
		out.append("fl=" + MathUtil.toHex(RandomUtil.randInt(0, Integer.MAX_VALUE - 1)));
		out.append("\n");
		out.append("h=fake.cloudflare.local");
		out.append("\n");
		out.append("ip=" + request.ip());
		out.append("\n");
		out.append("ts=" + System.currentTimeMillis());
		out.append("\n");
		out.append("visit_scheme=" + request.protocolScheme());
		out.append("\n");
		out.append("uag=" + request.header("User-Agent", URLUtils.STANDARD_USERAGENT));
		out.append("\n");
		out.append("colo=BUD");
		out.append("\n");
		out.append("sliver=none");
		out.append("\n");
		out.append("http=" + request.protocolDefinition().toLowerCase());
		out.append("\n");
		out.append("loc=AN");
		out.append("\n");
		if (request.protocolScheme().equalsIgnoreCase("https")) {
			out.append("tls=" + request.socket_tlsVersion());
			out.append("\n");
		}
		out.append("sni=plaintext");
		out.append("\n");
		out.append("warp=off");
		out.append("\n");
		out.append("gateway=off");
		out.append("\n");
		out.append("rbi=off");
		out.append("\n");
		out.append("kex=X25519");
		out.append("\n");
		return new Response(200, createStream(out.toString()));
	}

}
